package com.zhny.protocol_v3_008.network;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhny.meddleware.IMessenger;
import com.zhny.meddleware.Mediator;
import com.zhny.protocol_v3_008.constant.PTypeCode;
import com.zhny.protocol_v3_008.constant.ResponseCode;
import com.zhny.protocol_v3_008.model.Datagram;
import com.zhny.protocol_v3_008.model.Reply;
import com.zhny.util.BytesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * 接受sender当前发送的数据报类型
 * 接受网络中传来的信息，通知发送者和视图
 *
 * @author Create By Alex On 2018/4/8 14:39
 */
@Component
public class Receiver extends AbsNetwork implements IMessenger {
    private Integer currentType;
    private Reply reply;
    @Autowired
    private Mediator mediator;


    synchronized void receive() {
        mediator.sendMsgToView("响应区", null);
        try {
            byte[] headByte = new byte[Datagram.HEAD_SIZE];
            is.read(headByte);

            Datagram datagram = new Datagram();

            byte[] b_packageType = new byte[Datagram.PACKAGE_TYPE_SIZE];
            is.read(b_packageType);
            short packageType = BytesUtils.toShort(b_packageType);
            datagram.setPackageType(packageType);

            byte[] b_date = new byte[Datagram.DATE_SIZE];
            is.read(b_date);
            long date = (long) BytesUtils.toInt(b_date) * 1000;
            Date d = new Date(date);
            datagram.setDate((int) (System.currentTimeMillis() / 1000));

            byte[] b_deviceFlag = new byte[Datagram.DEVICE_FLAG_SIZE];
            is.read(b_deviceFlag);
            int deviceFlag = BytesUtils.toInt(b_deviceFlag);
            datagram.setDeviceFlag(deviceFlag);

            byte[] b_deviceType = new byte[Datagram.DEVICE_TYPE_SIZE];
            is.read(b_deviceType);
            short deviceType = BytesUtils.toShort(b_deviceType);
            datagram.setDeviceType(deviceType);

            byte[] b_dataLength = new byte[Datagram.DATA_LENGTH_SIZE];
            is.read(b_dataLength);
            short dataLength = BytesUtils.toShort(b_dataLength);
            datagram.setDataLength(dataLength);

            byte[] data = new byte[dataLength];
            is.read(data);
            String str = new String(data);

            byte[] b_crc = new byte[Datagram.CRC_SIZE];
            is.read(b_crc);
//            long CRC32 = BytesUtils.toLong(b_crc);


            byte[] endByte = new byte[Datagram.END_SIZE];
            is.read(endByte);


            ObjectMapper mapper = new ObjectMapper();
            reply = mapper.readValue(data, Reply.class);

            datagram.setData(reply.toString());

            mediator.sendMsgToView(datagram.toString(),datagram.toMap());

        } catch (IOException e) {
            e.printStackTrace();
        }


        switch (reply.getCode()) {
            case ResponseCode.FAIL:
            case ResponseCode.BASE_FLAG_ERROR:
            case ResponseCode.CRC_ERROR:
            case ResponseCode.END_ERROR:
            case ResponseCode.DATA_LENGTH_ERROR:
            case ResponseCode.DEV_FLAG_ERROR:
            case ResponseCode.DEV_TYPE_ERROR:
            case ResponseCode.HEAD_ERROR:
            case ResponseCode.TIME_ERROR:
            case ResponseCode.HEAD_TYPE_ERROR:
                mediator.sendMsgToView(PTypeCode.getName(currentType) + "失败!",null);
                mediator.sendMsgToSender(reply.getCode());
                break;
            default:
                mediator.sendMsgToView(PTypeCode.getName(currentType) + "成功!",null);
                break;
        }
    }

    public Mediator getMediator() {
        return mediator;
    }

    @Override
    public void handleMessage(int message) {
        currentType = message;
    }
}
